<template>
  <div>
    <el-breadcrumb separator-class='el-icon-arrow-right'>
      <el-breadcrumb-item v-bind:to='{ path: "/" }'>首页</el-breadcrumb-item>
      <el-breadcrumb-item>人才招聘</el-breadcrumb-item>
      <el-breadcrumb-item>修改招聘信息</el-breadcrumb-item>
    </el-breadcrumb>
    <div class='dict-main'>
      <el-tabs v-model='activeName' class='title-top'>
        <el-tab-pane label='修改招聘信息' name='first'>
          <div class='main-module charger'>
            <el-row class='posre'>
              <el-col :span='12' class='text-right search-top'>
                <el-button size='small' type="primary" icon="el-icon-check" @click="submitForm('ruleForm')"> 保 存
                </el-button>
                <el-button size='small' type='warning' icon='el-icon-back' @click='backToList()'>返回</el-button>
              </el-col>
            </el-row>
            <div>
              <el-row :gutter="20">
                <el-col :span="14" :offset="2">
                  <el-form :model="addForm" :rules="rules" ref="addForm" label-width="100px" label-suffix=":">
                    <el-form-item label="岗位名称" prop="jobName">
                      <el-input v-model="addForm.jobName"></el-input>
                    </el-form-item>
                    <el-form-item label="岗位类别" prop="jobType">
                      <el-radio-group v-model="addForm.jobType">
                        <el-radio label="1" border>技术大牛</el-radio>
                        <el-radio label="2" border>管理人才</el-radio>
                        <el-radio label="3" border>销售精英</el-radio>
                      </el-radio-group>
                    </el-form-item>
                    <el-form-item label="招聘人数" prop="hireNum">
                      <el-input-number v-model="addForm.hireNum" style="width: 100%" controls-position="right" :min="1"
                                       :max="99" :step="1"></el-input-number>
                    </el-form-item>
                    <el-form-item label='工作地点'  class='widthrow' prop="workplace">
                      <el-select  v-model='addForm.provinceCode' @change='findCitys' placeholder='--省--' style='width: 33%'>
                        <el-option
                          v-for='item in provinces'
                          :key='item.code'
                          :label='item.name'
                          :value='item.code'>
                        </el-option>
                      </el-select>
                      <el-select v-model='addForm.workplace' placeholder='--市--' style='width: 33%'>
                        <el-option
                          v-for='item in cities'
                          :key='item.code'
                          :label='item.name'
                          :value='item.code'>
                        </el-option>
                      </el-select>
                    </el-form-item>
                    <el-form-item label="生效日期" prop="startTime">
                      <el-date-picker
                        v-model="addForm.startTime"
                        type="date"
                        style="width: 50%;"
                        placeholder="生效日期选择">
                      </el-date-picker>
                    </el-form-item>

                    <el-form-item label="岗位标签" prop="label">
                      <!-- <el-input v-model="addForm.label"></el-input>-->
                      <el-tag
                        :key="tag"
                        type="warning"
                        v-for="tag in dynamicTags"
                        closable
                        :disable-transitions="false"
                        @close="handleClose(tag)">
                        {{tag}}
                      </el-tag>
                      <el-input
                        class="input-new-tag"
                        v-if="inputVisible"
                        v-model="inputValue"
                        ref="saveTagInput"
                        style="width: 200px;"
                        @keyup.enter.native="handleInputConfirm"
                        @blur="handleInputConfirm"
                      >
                      </el-input>
                      <el-button v-if="addBtVisible" class="button-new-tag" size="small" type="success"
                                 icon="el-icon-edit" @click="showInput">添加标签
                      </el-button>
                    </el-form-item>
                    <el-form-item label="招聘说明" prop="content" style="margin-bottom: 100px;width: 150%;">
                      <quill-editor v-model="addForm.content" style="height: 400px;" ref="myQuillEditor"
                                    :options="editorOption"
                                    @blur="onEditorBlur($event)" @focus="onEditorFocus($event)"
                                    @ready="onEditorReady($event)">
                      </quill-editor>
                    </el-form-item>
                  </el-form>
                </el-col>
              </el-row>
            </div>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>


  </div>
</template>

<script>
  import {
    editRecruit,
    loadRecruitDetail
  } from '@/api/recruit';
  import {
    getAllProvince,
    getCityByProvince
  } from '@/api/province';
  export default {
    data() {
      return {
        activeName: 'first',
        inputVisible: false,
        addBtVisible: true,
        inputValue: '',
        editorOption: {
          modules: {
            toolbar: [
              ['bold', 'italic', 'underline', 'strike', 'blockquote'],        // toggled buttons
              [{'header': 1}, {'header': 2}],
              [{'size': ['small', false, 'large', 'huge']}],
              [{'color': []}, {'background': []}],
              [{'list': 'ordered'}, {'list': 'bullet'}],
              [{'indent': '-1'}, {'indent': '+1'}],
              ['clean']
            ]
          }
        },
        dynamicTags: [],
        provinces: [],
        cities: [],
        addForm: {
          jobName: '',
          jobType: '1',
          workplace: '',
          hireNum: 1,
          label: '',
          startTime: '',
          content: ''
        },
        rules: {
          jobName: [
            {required: true, message: '请输入岗位名称', trigger: 'blur'},
            {min: 3, max: 20, message: '长度在 3 到 20 个字符', trigger: 'blur'}
          ],
          jobType: [
            {required: true, message: '请选岗位类别', trigger: 'blur'}
          ],
          workplace: [
            {required: true, message: '请选择工作地点', trigger: 'change'}
          ],
          hireNum: [
            {required: true, message: '请输入招聘人数', trigger: 'blur'}
          ],
          startTime: [
            {required: true, message: '请填写生效日期', trigger: 'blur'}
          ],
          content: [
            {required: true, message: '请填写招聘信息', trigger: 'blur'}
          ]
        }
      };
    },
    filters: {},
    methods: {
      onEditorBlur(quill) {
        console.log('editor blur!', quill)
      },
      onEditorFocus(quill) {
        console.log('editor focus!', quill)
      },
      onEditorReady(quill) {
        console.log('editor ready!', quill)
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      backToList() {
        this.$router.push({
          path: '/team/recruit'
        })
      },
      handleClose(tag) {
        this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
      },
      showInput() {
        this.inputVisible = true;
        this.addBtVisible = false;
        this.$nextTick(_ => {
          this.$refs.saveTagInput.$refs.input.focus();
        });
      },
      handleInputConfirm() {
        let inputValue = this.inputValue;
        if (inputValue) {
          this.dynamicTags.push(inputValue);
        }
        this.inputVisible = false;
        this.addBtVisible = true;
        this.inputValue = '';
      },
      findCitys (val) {
        getCityByProvince(val).then(response => {
          if (response.code == 0) {
            this.cities = response.data;
          } else {
            this.$message({
              message: response.message,
              type: 'error'
            });
          }
        });
      },
      loadRecruitDetail () {
        loadRecruitDetail(this.$route.params.recruitId).then(response => {
          if (response.code !== 0) {
            this.$message({
              message: response.message,
              type: 'error'
            });
          } else {
            this.addForm = response.data;
            this.dynamicTags= response.data.label.split(';');
            this.findCitys(response.data.provinceCode);
          }
        });
      },
      submitForm() {
        var that = this;
        this.$refs.addForm.validate((valid) => {
          if (valid) {
            that.addForm.label=that.dynamicTags.join(';');
            editRecruit(this.addForm).then(response => {
              if (response.code == 0) {
                this.$message({
                  message: '发布招聘信息成功',
                  type: 'success'
                });
                this.backToList();
              } else {
                this.$message({
                  message: response.message,
                  type: 'error'
                });
              }
            });
          } else {
            return false;
          }
        })
      }
    },
    watch: {
      dynamicTags: function (newTags) {
        if (newTags.length >= 3) {
          this.addBtVisible = false;
        } else {
          this.addBtVisible = true;
        }
      }
    },
    created: function () {
      this.loadRecruitDetail();
      getAllProvince().then(response => {
        this.provinces = response.data;
      });
    }
  };
</script>

<style>
  .el-tag + .el-tag {
    margin-left: 10px;
  }

  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }

  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
</style>
